import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_jd/pages/order/controller.dart';
import 'package:flutter_jd/routes/routing_constants.dart';
import 'package:flutter_jd/pages/goods/detail/view.dart';
import 'package:flutter_jd/pages/search/view.dart';
import 'package:flutter_jd/ui/widgets/indicator/tabbar_custom.dart';
import 'package:flutter_jd/ui/widgets/kit.dart';
import 'package:get/route_manager.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

// 订单
class Order extends GetView<OrderController> {
  @override
  Widget build(BuildContext context) {
    Get.put(OrderController(initTabIndex: Get.arguments['initTabIndex']));
    // Get.lazyPut(() => OrderController(initTabIndex: Get.find()));

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark,
      child: Theme(
        data: ThemeData(
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent,
        ),
        child: Scaffold(
          backgroundColor: Colors.white,
          body: SafeArea(
            bottom: false,
            child: Column(
              children: [
                _buildSearchBar(),
                Expanded(
                  child: DefaultTabController(
                    length: controller.tabs.length,
                    initialIndex: 0,
                    child: Column(
                      children: <Widget>[
                        Container(
                          height: 50.h,
                          width: double.infinity,
                          color: Color(0xFFF3F3F3),
                          child: TabBarCustom(
                            tabs: controller.tabs,
                            tabController: controller.tabController,
                          ),
                        ),
                        _buildOrderList()
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  _buildItem(int index, int cIndex) {
    var item = controller.productList[index]['children'][cIndex];

    return Container(
      padding: EdgeInsets.only(left: 15.w, right: 15.w),
      child: Row(
        children: [
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                GestureDetector(
                  onTap: () {
                    Get.to(
                      () => GoodsDetail(arguments: {
                        'imgUrl': item['src'],
                        'name': item['name'],
                        'price': item['price'],
                      }),
                    );
                  },
                  child: Container(
                    width: 105.w,
                    height: 105.h,
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(5.r),
                      child: Image.asset(item['src']),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(left: 8.w),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Container(
                          margin: EdgeInsets.only(top: 5.h),
                          child: Text(
                            item['name'],
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              color: Color(0xFF333333),
                              fontSize: 13.sp,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Container(
                  height: double.infinity,
                  margin: EdgeInsets.only(left: 10.w, right: 5.w),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        margin: EdgeInsets.only(top: 4.h),
                        child: Text(
                          '¥',
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            color: Color(0xFF333333),
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                      Text(
                        item['price'].toString(),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          color: Color(0xFF333333),
                          fontSize: 15.sp,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // 构建头部按钮
  _buildHeadLeftBtn() {
    return GestureDetector(
      onTap: () {
        Get.back();
      },
      child: Container(
        width: double.infinity,
        height: double.infinity,
        color: Colors.transparent,
        child: Kit.iconFont(0xe671, Color(0xFF333333), 18.sp),
      ),
    );
  }

  _buildSearchBar() {
    return Container(
      height: 40.h,
      padding: EdgeInsets.only(right: 10.w, bottom: 5.h),
      color: Colors.white,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            width: 35.w,
            child: _buildHeadLeftBtn(),
          ),
          Expanded(
            child: Container(
              height: 32.h,
              decoration: BoxDecoration(
                color: Color(0xFFF5F5F5),
                borderRadius: BorderRadius.circular(32.r),
              ),
              child: Container(
                padding: EdgeInsets.only(left: 12.w, right: 12.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Kit.iconFont(0xe692, Color(0xFF999999), 15.sp),
                    Expanded(
                      child: GestureDetector(
                        onTap: () {
                          Get.to(
                            () => SearchPage(),
                            transition: Transition.downToUp,
                            fullscreenDialog: true,
                          );
                        },
                        child: Container(
                          margin: EdgeInsets.only(left: 8.w),
                          child: Text(
                            '搜索我的订单',
                            style: TextStyle(
                              color: Color(0xFF999999),
                              fontSize: 12.5.sp,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
          Row(
            children: [
              GestureDetector(
                onTap: () {
                  Get.toNamed(MessageRoute);
                },
                child: Container(
                  color: Colors.transparent,
                  padding: EdgeInsets.only(left: 10.w),
                  child: Kit.iconFont(0xe8b8, Color(0xFF999999), 22.sp),
                ),
              )
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildOrderList() {
    return Expanded(
      child: TabBarView(
        controller: controller.tabController,
        children: controller.tabs.map((item) {
          return Container(
            color: Color(0xFFF3F3F3),
            child: ListView(
              children: List.generate(
                controller.productList.length,
                (index) => Container(
                  margin: EdgeInsets.only(
                    top: index == 0 ? 10.h : 0,
                    bottom: 12,
                  ),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10.r),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        width: double.infinity,
                        padding: EdgeInsets.only(
                          top: 15.h,
                          bottom: 12.h,
                        ),
                        child: Row(
                          children: [
                            Container(
                              margin: EdgeInsets.only(left: 12.w),
                              child: Text(
                                controller.productList[index]['title'],
                                style: TextStyle(
                                  fontSize: 14.sp,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      Column(
                        children: List.generate(
                          controller.productList[index]['children'].length,
                          (cIndex) => Container(
                            height: 130.h,
                            color: Colors.white,
                            child: _buildItem(
                              index,
                              cIndex,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          );
        }).toList(),
      ),
    );
  }
}
